﻿using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 仪表识别2
{
    class TabManager:Control
    {
        private TabControl tabControl;
        //放大镜组件
        private Magnify magnify;
        private Form1 form1;
        public TabManager(TabControl tabControl)
        {
            this.tabControl = tabControl;
        }
        public void add(TabPage tabPage)
        {
            bool extis = false;
            foreach(TabPage tp in tabControl.TabPages)
            {
                if (tp.Text == tabPage.Text)
                {
                    extis = true;
                    tp.Select();
                    break;
                }
            }
            if(!extis)
                tabControl.Invoke((DummyDelegate)delegate {
                    tabControl.TabPages.Add(tabPage);
                });
        }
        public delegate void DummyDelegate();
        public void add(TabPage tabPage,int index)
        {
            bool extis = false;
            foreach (TabPage tp in tabControl.TabPages)
            {
                if (tp.Text == tabPage.Text)
                {
                    extis = true;
                    tp.Select();

                    break;
                }
            }
            if (!extis)
            tabControl.Invoke((DummyDelegate)delegate {
                tabControl.TabPages.Insert(index, tabPage);
            });

        }
        public void remove(string tabName)
        {
            TabPage tabPage = null;
            foreach (TabPage tp in tabControl.TabPages)
            {
                if (tp.Text == tabName)
                {
                    tabPage = tp;
                    break;
                }
            }
            if (tabPage!=null)
                tabControl.Invoke((DummyDelegate)delegate
                {
                    tabControl.TabPages.Remove(tabPage);

                });
        }

        public void addTabPageWithPicBox(string title, Bitmap bitMap)
        {
            bool extis = false;
            int index = 0;
            foreach (TabPage tp in tabControl.TabPages)
            {
                if (tp.Text == title)
                {
                    extis = true;
                    PictureBox p = (PictureBox)tp.Controls[0];
                    p.Image = bitMap;
                    //tabControl.SelectedIndex = index;
                    break;
                }
                index++;
            }
            if (!extis)
            {
                TabPage tabPage = new TabPage();
                tabPage.Text = title;
                tabPage.BackColor = Color.Transparent;
                PictureBox pictureBox = new PictureBox();
                pictureBox.Dock = DockStyle.Fill;
                pictureBox.Left = 0;
                pictureBox.Top = 0;
                pictureBox.Parent = tabPage;
                pictureBox.BorderStyle = BorderStyle.Fixed3D;
                pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox.BackColor = Color.White;
                if (bitMap != null)
                    pictureBox.Image = bitMap;
                tabPage.Controls.Add(pictureBox);
                add(tabPage);
                //tabControl.SelectedIndex = tabControl.TabCount-1;

                //绑定事件
                pictureBox.MouseClick += new MouseEventHandler(picBoxClickEvent);
                //放大镜
                pictureBox.MouseMove += new MouseEventHandler(picBoxMouseMoveEvent);
                //添加右键菜单
                //TODO 

               

            }
        }
        public void picBoxClickEvent(object sender, MouseEventArgs e)
        {
            if (AppConstant.markModel)
            {
               
                MouseEventArgs eventArgs = e;
                PictureBox p = (PictureBox)sender;
                int w = p.Image.Width;
                int h = p.Image.Height;
                int cw = p.Width;
                int ch = p.Height;
                float bl_w = ((float)cw) / ((float)w);
                float bl_h = ((float)ch) / ((float)h);
                float bl = Math.Min(bl_w, bl_h);
                int vw = (int)(w * bl);
                int vh = (int)(h * bl);
                int x0 = (cw - vw) / 2;
                int y0 = (ch - vh) / 2;
                int x = (int)((eventArgs.Location.X - x0) / bl);
                int y = (int)((eventArgs.Location.Y - y0) / bl);
                Image<Bgr, byte> img = new Image<Bgr, byte>(new Bitmap(p.Image));

                img.Draw(new CircleF(new PointF(x, y), 2), new Bgr(AppConstant.choseColor), 3);

                p.Image = img.Bitmap;
                
                if (AppConstant.markTimes==1)
                {
                    ImageParams.pointEnd = new Point(x, y);
                    Log.info("终止刻度标记成功:" + "(" + x + "," + y + ")");
                    AppConstant.markModel = false;
                    AppConstant.markTimes = 0;
                    Form1.form.tipLable.Text = "刻度标记成功";
                }
                else
                {
                    ImageParams.pointStart = new Point(x, y);
                    Log.info("起始刻度标记成功:" + "(" + x + "," + y + ")");
                    AppConstant.markTimes++;
                }
            }
           if (AppConstant.pointerMarkModel)
            {
                MouseEventArgs eventArgs = e;
                PictureBox p = (PictureBox)sender;
                int w = p.Image.Width;
                int h = p.Image.Height;
                int cw = p.Width;
                int ch = p.Height;
                float bl_w = ((float)cw) / ((float)w);
                float bl_h = ((float)ch) / ((float)h);
                float bl = Math.Min(bl_w, bl_h);
                int vw = (int)(w * bl);
                int vh = (int)(h * bl);
                int x0 = (cw - vw) / 2;
                int y0 = (ch - vh) / 2;
                int x = (int)((eventArgs.Location.X - x0) / bl);
                int y = (int)((eventArgs.Location.Y - y0) / bl);
                Image<Bgr, byte> img = new Image<Bgr, byte>(new Bitmap(p.Image));

                img.Draw(new CircleF(new PointF(x, y), 2), new Bgr(AppConstant.choseColor), 3);

                p.Image = img.Bitmap;

                if (AppConstant.markTimes == 1)
                {
                    ImageParams.pointerEnd = new Point(x, y);
                    Log.info("指针远圆心端标记成功:" + "(" + x + "," + y + ")");
                    AppConstant.pointerMarkModel = false;
                    AppConstant.markTimes = 0;
                    Form1.form.tipLable.Text = "指针标记成功";
                }
                else
                {
                    ImageParams.pointerStart= new Point(x, y);
                    Log.info("指针近圆心端标记成功:" + "(" + x + "," + y + ")");
                    AppConstant.markTimes++;
                }
            }
           
            

        }
        //鼠标移动事件，用于放大镜和坐标显示
        public void picBoxMouseMoveEvent(object sender, MouseEventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            int w = pb.Image.Width;
            int h = pb.Image.Height;
            int cw = pb.Width;
            int ch = pb.Height;
            float bl_w = ((float)cw) / ((float)w);
            float bl_h = ((float)ch) / ((float)h);
            float bl = Math.Min(bl_w, bl_h);
            int vw = (int)(w * bl);
            int vh = (int)(h * bl);
            int x0 = (cw - vw) / 2;
            int y0 = (ch - vh) / 2;
            int x = (int)((e.Location.X - x0) / bl); 
            int y = (int)((e.Location.Y - y0) / bl);
            Form1.form.locationLabel.Text = x + "," + y;
            PictureBox picBox = (PictureBox)sender;
           
            if (AppConstant.showMannify)
            {
                Point p = e.Location;

                if (magnify == null)
                {
                    magnify = new Magnify(picBox, 3);
                }
                else if(magnify.srcBox != picBox)
                {
                    magnify = new Magnify(picBox, 3);
                }
                magnify.enlarge(p.X, p.Y);
            }
        }

    }
}
